package org.hq.web.security;


import org.hq.web.sql.mapper.UserMapper;
import org.hq.web.sql.pojo.WebUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 系统登录服务用户创建服务类
 * 查询数据库用户校验
 */
@Component
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("用户 = "+username);
        //获取数据库用户
        WebUser webUser = userMapper.selectUserId(username);
        //判断用户是否存在
        if (webUser==null) {
            throw new UsernameNotFoundException("用户不存在"+username);
        }
        return new LoginUser(webUser);
    }
}
